dojo.provide("apt.DataRow");
dojo.require("apt.Event");
dojo.require("dijit._WidgetBase");
dojo.require("dijit._TemplatedMixin");

dojo.declare("apt.DataRow", [dijit._WidgetBase, dijit._TemplatedMixin], {
    templateString: dojo.cache('apt.templates', 'DataRow.html'),
    widgetsInTemplate: true,
    resourceName: '',
    parent: '',
    days: '',
    start: 24,
    end: 0,

    constructor: function (data) {
        this.data = [];
        this.inherited(arguments);
        dojo.safeMixin(this, data);

        this.baseClass = "datarow-" + this.resourceName;

        // Initialize _non-static_ array parameters
        this.events = [];
        this.availabilities = [];
    },

    postCreate: function () {

        // Renedr availabilities
        if (this.data.hasOwnProperty('availabilities')) {
            for (var i = 0; i < this.data.availabilities.length; i++) {
                this.addAvailability(this.data.availabilities[i]);
            }
            ;
        }

        // Report own start & end back to parent
        this.parent.getDataRowStartAndEnd(Math.floor(this.start), Math.floor(this.end) + 1);

        // Render Breaks
        if (this.data.hasOwnProperty('breaks')) {
            for (var i = 0; i < this.data.breaks.length; i++) {
                this.addBreak(this.data.breaks[i]);
            }
            ;
        }
        ;

        // Render reservations (only for Smans that have an availability set for this day)
        if (this.data.hasOwnProperty('reservations')) {
            for (var i = this.data.reservations.length - 1; i >= 0; i--) {
                this.addReservation(this.data.reservations[i]);
            }
            ;
        }
    },

    addReservation: function (e) {
        this.renderEvent(e, 'reservation');
    },

    addBreak: function (e) {
        if (typeof e.StartTimeJSDate == "undefined") {
            e.StartTimeJSDate = (e.WorkingDate + " " + e.StartHour).parseISO8601();
        }
        if (typeof e.EndTimeJSDate == "undefined") {
            e.EndTimeJSDate = (e.WorkingDate + " " + e.EndHour).parseISO8601();
        }

        this.renderAvailability(e, 'lunch');
    },

    addAvailability: function (e) {
        //debugger;
        // ?? pay attention time start + end ? is it avaibility
        //e.StartTimeJSDate = e.StartTime.parseISO8601();
        //e.EndTimeJSDate = e.EndTime.parseISO8601();
        if (typeof e.StartTimeJSDate == "undefined") {
            e.StartTimeJSDate = (e.WorkingDate + " " + e.StartHour).parseISO8601();
        }
        if (typeof e.EndTimeJSDate == "undefined") {
            e.EndTimeJSDate = (e.WorkingDate + " " + e.EndHour).parseISO8601();
        }

        if (e.StartTimeJSDate.getHours() < this.start) {
            this.start = e.StartTimeJSDate.getHours();
        }
        if (e.EndTimeJSDate.getHours() > this.end) {
            this.end = e.EndTimeJSDate.getHours();
        }

        if (typeof e.StartTimeJSDate == "undefined") {
            e.StartTimeJSDate = (e.WorkingDate + " " + e.StartHour).parseISO8601();
        }
        if (typeof e.EndTimeJSDate == "undefined") {
            e.EndTimeJSDate = (e.WorkingDate + " " + e.EndHour).parseISO8601();
        }

        this.start = e.StartTimeJSDate.getHours() + e.StartTimeJSDate.getMinutes() / 60;
        this.end = e.EndTimeJSDate.getHours() + e.EndTimeJSDate.getMinutes() / 60;
        this.renderAvailability(e, 'available');
    },

    renderAvailability: function (event, type) {

        startTime = this.parent.getLeftOffsetForTime(event.StartTimeJSDate.getHours(), event.StartTimeJSDate.getMinutes());
        endTime = this.parent.getRightOffsetForTime(event.EndTimeJSDate.getHours(), event.EndTimeJSDate.getMinutes());

        var e = new apt.Event({
            className: ' ' + type + ' ' + (Math.random() * 1000 << 0),
            e: event,
            style: {
                position: 'absolute',
                left: (startTime) + "px",
                right: (endTime) + "px"
            }
        });

        this.availabilities.push(e);

        e.placeAt(this.availabilityNode, 'last');
    },

    resize: function () {
        this.updateAvailabilities();
        this.updateEvents();
    },

    updateAvailabilities: function () {
        for (var i = this.availabilities.length - 1; i >= 0; i--) {
            var e = this.availabilities[i];
            var lpx = this.parent.getLeftOffsetForTime(e.e.StartTimeJSDate.getHours(), e.e.StartTimeJSDate.getMinutes());
            var rpx = this.parent.getRightOffsetForTime(e.e.EndTimeJSDate.getHours(), e.e.EndTimeJSDate.getMinutes());
            dojo.style(e.domNode, {
                left: lpx + "px",
                right: rpx + "px"
            })
        }
        ;
    },

    updateEvents: function () {
        //debugger; --> test for change position of left to box-side --> ok
        //for (var i = this.events.length - 1; i >= 0; i--){
        for (var i = 0; i < this.events.length; i++) {
            var event = this.events[i];
            var startTimePixelOffset, endTimePixelOffset;

            // Check if event starts before today or before today's availability
            //if (event.e.StartTimeJSDate <= this.parent.date || event.e.StartTimeJSDate.getHours() + event.e.StartTimeJSDate.getMinutes()/60 < this.start) {
            //  startTimePixelOffset = this.parent.getLeftOffsetForTime(this.start, 0);
            //} else {
            startTimePixelOffset = this.parent.getLeftOffsetForTime(event.e.StartTimeJSDate.getHours(), event.e.StartTimeJSDate.getMinutes());
            //}

            // Check if event ends after today or after today's availability
            //if (event.e.EndTimeJSDate >= new Date(this.parent.date.getTime()+86400000) || event.e.EndTimeJSDate.getHours() + event.e.EndTimeJSDate.getMinutes()/60 >= this.end) {
            //  endTimePixelOffset = this.parent.getRightOffsetForTime(this.end, 0);
            //} else {
            endTimePixelOffset = this.parent.getRightOffsetForTime(event.e.EndTimeJSDate.getHours(), event.e.EndTimeJSDate.getMinutes());
            //}

            dojo.style(event.domNode, {
                left: (startTimePixelOffset) + "px",
                right: (endTimePixelOffset) + "px"
            })
        }
        ;
    },

    renderEvent: function (event, type) {
        startTime = this.parent.getLeftOffsetForTime(event.StartTimeJSDate.getHours(), event.StartTimeJSDate.getMinutes());
        endTime = this.parent.getRightOffsetForTime(event.EndTimeJSDate.getHours(), event.EndTimeJSDate.getMinutes());

        var e = new apt.Event({
            className: 'planned event ' + type,
            e: event,
            type: type,
            style: {
                position: 'absolute',
                left: startTime + 'px',
                right: endTime + 'px'
            },
            parent: this
        });

        this.events.push(e);

        e.placeAt(this.eventsNode, 'last');
    }

});
